<template>
  <div class="achievement-container">
    <!-- 列表展示结对消息 -->
    <van-list>
      <div v-if="jieduiList.length === 0" class="no-message">
        暂时没有消息
      </div>
      <van-cell
          v-for="item in jieduiList"
          :key="item.applicationId"
          :title="item.description"
          :label="formatDate(item.createTime)"
          @click="showDetail(item)"
          class="jiedui-item"
      >
        <template #value>
          <van-tag :color="getStatusColor(item.status)">
            {{ getStatusText(item.status) }}
          </van-tag>
        </template>
      </van-cell>
    </van-list>

    <!-- 弹窗 -->
    <van-dialog
        v-model:show="isDialogVisible"
        title="结对申请详情"
        show-cancel-button
        @confirm="handleApplication(true)"
        @cancel="handleApplication(false)"
        class="jiedui-dialog"
        close-on-click-overlay
        :cancel-button-text="'拒绝结队'"
        :confirm-button-text="'同意结队'"
        :confirm-button-color="'#7c2d7c'"
        :cancel-button-color="'#a5a4a4'"
    >
      <div class="dialog-content">
        <div class="dialog-info">
          <p>申请人姓名: {{ selectedApplication?.applicantName }}</p>
          <p>申请人工号: {{ selectedApplication?.userNumber }}</p>
          <p>申请描述: {{ selectedApplication?.description }}</p>
          <p>申请时间: {{ formatDate(selectedApplication?.createTime) }}</p>
        </div>
        <div class="dialog-photo">
          <img :src="selectedApplication?.photoPath" alt="结队照片" />
        </div>
      </div>
    </van-dialog>
  </div>
</template>

<script setup lang="ts">
import { getPendingApplicationsUsingGet, handleApplicationUsingPost } from "../../Api/jiaoshijiedui.ts";
import { showToast } from "vant";
import { onMounted, ref } from "vue";

// 数据列表
const jieduiList = ref([]);

// 弹窗是否显示
const isDialogVisible = ref(false);

// 当前选中的结对申请
const selectedApplication = ref<any>(null);

async function getJieDuiList() {
  try {
    const response = await getPendingApplicationsUsingGet();
    console.log(response.data.data);
    jieduiList.value = response.data.data;
  } catch (err) {
    showToast("获取数据失败");
  }
}

onMounted(() => {
  getJieDuiList();
});

function showDetail(item: any) {
  selectedApplication.value = item;
  isDialogVisible.value = true;
}

async function handleApplication(isAccepted: boolean) {
  if (!selectedApplication.value) return;

  try {
    const params = {
      applicationId: selectedApplication.value.applicationId,
      isAccepted,
    };
    await handleApplicationUsingPost(params);
    showToast(isAccepted ? "已接受结对申请" : "已拒绝结对申请");
    isDialogVisible.value = false;
    getJieDuiList(); // 刷新列表
  } catch (err) {
    showToast("处理失败");
  }
}

// 格式化时间
function formatDate(dateString: string) {
  const date = new Date(dateString);
  return date.toLocaleString();
}

// 获取状态文本
function getStatusText(status: number) {
  switch (status) {
    case 0:
      return "待处理";
    case 1:
      return "已接受";
    case 2:
      return "已拒绝";
    default:
      return "未知状态";
  }
}

// 获取状态颜色
function getStatusColor(status: number) {
  switch (status) {
    case 0:
      return "orange";
    case 1:
      return "green";
    case 2:
      return "red";
    default:
      return "gray";
  }
}
</script>

<style scoped>
.achievement-container {
  height: 100vh;
  overflow-y: auto;
  padding: 10px;
  background-color: #f5f5f5;
  //display: flex;
  //justify-content: center;
  //align-items: center;
}

.no-message {
  color: #999;
  font-size: 16px;
  text-align: center;
}

.jiedui-item {
  display: flex;
  align-items: center;
  padding: 10px;
  margin-bottom: 10px;
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.jiedui-photo {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  margin-right: 10px;
}

.jiedui-dialog {
  border-radius: 12px;
}

.dialog-content {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 15px;
}

.dialog-photo {
  width: 100px;
  height: 100px;
  overflow: hidden;
  margin-bottom: 15px;
}

.dialog-photo img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.dialog-info {
  text-align: center;
}

.dialog-info p {
  margin: 5px 0;
  font-size: 14px;
  color: #333;
}
</style>